//
//  GKDYPlayerViewController.m
//  GKDYVideo
//
//  Created by QuintGao on 2018/9/23.
//  Copyright © 2018 QuintGao. All rights reserved.
//

#import "LYouPlayerViewController.h"
#import "GoogleMobileAds/GoogleMobileAds.h"

@interface LYouPlayerViewController ()<GADInterstitialDelegate>

@property (nonatomic, strong) UILabel  *titleLable;
@property (nonatomic, strong) UIButton  *setButton; /** 设置按钮 */
@property (nonatomic, strong) UIButton  *searchButton; /** 搜索按钮 */
@property (nonatomic, strong) UIView  *redView;/** 小红点 */
@property (nonatomic,strong) GADInterstitial *adboadView;  /** 广告版位 */

@end

@implementation LYouPlayerViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = ColorWithHexRGB(0x1F1E23);
    self.gk_navigationBar.hidden = YES;
    self.gk_statusBarHidden = YES;
    
    [self.view addSubview:self.videoView];
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    WeakSelf(weakSelf);
    self.videoView.detailClickBlock = ^(LYouVideoModel * _Nonnull model) {
        if (weakSelf.pushToDetailVCBlock) {
            weakSelf.pushToDetailVCBlock(model);
        }
    };
    self.videoView.pushToBuyBlock = ^(NSString *isCHeck) {
        
        if (weakSelf.pushToBuyMemVCBlock) {
            weakSelf.pushToBuyMemVCBlock(isCHeck);
        }
    };
    /** 收藏 */
    self.videoView.clickFavorClickBlock = ^(LYouVideoModel *model,NSString * _Nonnull isFavor) {
        if ([isFavor isEqualToString:@"1"]) {
            /** 收藏视频 */
            [[LYVideoInfoManager share]addVideoToLocalData:model];
            
        }else{
            /** 取消收藏 */
            [[LYVideoInfoManager share]deleteVideoToLocalData:model];
        }
    };
    /** 插播广告 */
    self.videoView.showAdboadBlock = ^{
        weakSelf.adboadView = [[GADInterstitial alloc]initWithAdUnitID:LYHomeADId];
        weakSelf.adboadView.delegate = weakSelf;
        [weakSelf.adboadView loadRequest:[GADRequest request]];
    };
    
    [self.view addSubview:self.titleLable];
    
    CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
    CGFloat hight = 22;
    if (safeBottem>0) {
        hight = 42;
    }
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(hight);
        make.height.mas_equalTo(40);
    }];
    [self.view addSubview:self.setButton];
    [self.setButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(25);
        make.top.equalTo(self.view).offset(hight+3);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(25);
    }];
    [self.view addSubview:self.searchButton];
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-25);
        make.top.equalTo(self.view).offset(hight+4);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(25);
    }];
    [self.view addSubview:self.redView];
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-23);
        make.top.equalTo(self.view).offset(hight+3);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(10);
    }];
    self.redView.layer.cornerRadius = 5;
}

#pragma mark ===========广告代理
-(void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error{
    NSLog(@"%@", [error localizedDescription]);
}

-(void)interstitialDidReceiveAd:(GADInterstitial *)ad{
    [self.adboadView presentFromRootViewController:self];
    [self.videoView pause];
}

-(void)interstitialWillDismissScreen:(GADInterstitial *)ad{
    [self.videoView resume];
}

#pragma mark - 懒加载
- (LYouVideoView *)videoView {
    if (!_videoView) {
        _videoView = [[LYouVideoView alloc] initWithVC:self isPushed:NO];
    }
    return _videoView;
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [UILabel new];
        _titleLable.text = @"推荐";
        [_titleLable setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        _titleLable.textAlignment = NSTextAlignmentCenter;
        _titleLable.textColor = [UIColor whiteColor];
    }
    return _titleLable;
}

#pragma mark - 设置按钮
- (UIButton *)setButton {
    if (!_setButton) {
        _setButton = [UIButton new];
        [_setButton setBackgroundImage:[UIImage imageNamed:@"setting_btn"] forState:UIControlStateNormal];
        [_setButton setBackgroundImage:[UIImage imageNamed:@"setting_btn"] forState:UIControlStateHighlighted];
        _setButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [_setButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_setButton addTarget:self action:@selector(setBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _setButton;
}

-(void)setBtnClick:(UIButton *)sender{
    if (self.pushToSetVCBlock) {
        self.pushToSetVCBlock();
    }
}

#pragma mark - 搜索按钮
- (UIButton *)searchButton {
    if (!_searchButton) {
        _searchButton = [UIButton new];
        [_searchButton setBackgroundImage:[UIImage imageNamed:@"search_btn"] forState:UIControlStateNormal];
        [_searchButton setBackgroundImage:[UIImage imageNamed:@"search_btn"] forState:UIControlStateHighlighted];
        _searchButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [_searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_searchButton addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}

-(UIView *)redView{
    if (!_redView) {
        _redView = [UIView new];
        _redView.backgroundColor = [UIColor redColor];
    }
    return _redView;
}

-(void)searchButtonClick:(UIButton *)sender{
    if (self.pushToSearchVCBlock) {
        self.pushToSearchVCBlock();
    }
}

- (void)dealloc {
    [self.videoView destoryPlayer];
}

@end
